package com.juma.cartridge.retryque;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.juma.cartridge.retryque.dao.RetryQueueDao;

public class DefaultRetryQueueAdmin implements RetryQueueAdmin {

	@Resource
	private RetryQueueDao retryQueueDao;
	
	@Override
	public boolean recoverTask(Set<Long> taskId) {
		if (taskId==null||taskId.isEmpty()) {
			return false;
		}
		return retryQueueDao.recover(taskId) > 0;
	}

	@Override
	public boolean stopTask(Set<Long> taskId) {
		if (taskId==null||taskId.isEmpty()) {
			return false;
		}
		return retryQueueDao.broken(taskId)>0;
	}

	
	@Override
	public boolean clearTask(Set<Long> taskId) {
		if (taskId==null||taskId.isEmpty()) {
			return false;
		}
		return retryQueueDao.clear(taskId)>0;
	}

	
	public void setRetryQueueDao(RetryQueueDao retryQueueDao) {
		this.retryQueueDao = retryQueueDao;
	}

	@Override
	public List<RetryTask> getAllTasks() {
		return retryQueueDao.queryAll();
	}

    @Override
    public Map<Long, RetryTask> getMultiTasks(Set<Long> taskId) {
        Map<Long,RetryTask> map = new HashMap<Long,RetryTask>();
        if(taskId!=null && !taskId.isEmpty()) {
            List<RetryTask> tasks = retryQueueDao.getTaskByIds(taskId);
            for(RetryTask task:tasks) {
                map.put(task.getId(), task);
            }
        }
        return map;
    }
	
}
